# import connector.datebasedriver
# from connector.datebasedriver import datebasedriver
#
# def get_all_students(driver: datebasedriver) -> list:
#     results: list = driver.select_all("select * from student_information")
#     return results
#
#
# def insert_students(driver: datebasedriver, student_Sno: int, student_Sname: str, student_Ssex: str, student_Sclass: str, student_Birth: str, student_Saddress: str, student_Sdept: str, student_Stime: str) -> int:
#     return driver.insert(sql='insert into student_information(Sno,Sname,Ssex,Sclass,Birth,Saddress,Sdept,Stime) values ("{}","{}","{}","{}","{}","{}","{}","{}")'.format(student_Sno,student_Sname,student_Ssex,student_Sclass,student_Birth,student_Saddress,student_Sdept,student_Stime))
#
#
# def user_search_students_by_student_Sno(driver: datebasedriver, student_Sno: int)  -> tuple:
#     results: tuple = driver.select_all('select * from student_information where Sno = "{}"'.format(student_Sno))
#     return results
#
# def user_search_students_by_student_Sname(driver: datebasedriver, student_Sname: str)  -> tuple:
#     results: tuple = driver.select_all('select * from student_information where Sname = "{}"'.format(student_Sname))
#     return results
#
# def student_list(driver:datebasedriver)->list:
#     return driver.select_all("select * from student_information", log=True)
import connector.datebasedriver


class StudentDao:
    def __init__(self):
        try:
            self.driver = connector.datebasedriver.datebasedriver()
        except Exception as e:
            print(f"Error initializing datebasedriver: {e}")
            self.driver = None

    def insert_students(self, student_Sno, student_Sclass, student_Sname, student_Ssex, student_Saddress, student_Birth, student_Sdept, student_Stime):
        if self.driver is None:
            print("Driver not initialized")
            return
        sql = f"INSERT INTO student_information (Sno, Sclass, Sname, Ssex, Saddress, Birth, Sdept, Stime) VALUES ({student_Sno}, '{student_Sclass}', '{student_Sname}', '{student_Ssex}', '{student_Saddress}', '{student_Birth}', '{student_Sdept}', '{student_Stime}')"
        return self.driver.insert(sql, log=True)

    def user_search_students_by_student_Sno(self, student_Sno: int) -> tuple:
        if self.driver is None:
            print("Driver not initialized")
            return ()
        sql = f'SELECT * FROM student_information WHERE Sno = {student_Sno}'
        return self.driver.select_all(sql, log=True)

    def get_all_students(self) -> list:
        if self.driver is None:
            print("Driver not initialized")
            return []
        sql = 'SELECT * FROM student_information'
        return self.driver.select_all(sql, log=True)

    def update_student(self, student_Sno, student_Sclass, student_Sname, student_Ssex, student_Saddress, student_Birth, student_Sdept, student_Stime):
        if self.driver is None:
            print("Driver not initialized")
            return
        sql = f"""
           UPDATE student_information
           SET Sclass = '{student_Sclass}', Sname = '{student_Sname}', Ssex = '{student_Ssex}', Saddress = '{student_Saddress}', Birth = '{student_Birth}', Sdept = '{student_Sdept}', Stime = '{student_Stime}'
           WHERE Sno = {student_Sno}
           """
        return self.driver.update(sql, log=True)

    def get_student_by_id(self, student_Sno: int) -> dict:
        if self.driver is None:
            print("Driver not initialized")
            return None
        result = self.user_search_students_by_student_Sno(student_Sno)
        if result:
            student_info = result[0]  # Assuming result is a list of tuples
            return {
                'Sno': student_info[0],
                'Sclass': student_info[1],
                'Sname': student_info[2],
                'Ssex': student_info[3],
                'Saddress': student_info[4],
                'Birth': student_info[5],
                'Sdept': student_info[6],
                'Stime': student_info[7]
            }
        else:
            return None

    def delete_student(self, student_id: int):
        if self.driver is None:
            print("Driver not initialized")
            return
        sql = f'DELETE FROM student_information WHERE Sno = {student_id}'
        return self.driver.delete(sql, log=True)